//
//  GRNewsDetailViewController.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/9/5.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRNewsDetailViewController.h"
#import <WebKit/WebKit.h>
#import "GRStoreAction.h"
#import "GRNewsModel.h"
@interface GRNewsDetailViewController ()<WKNavigationDelegate>
@property(nonatomic,strong)WKWebView * wkWebView;
@property(nonatomic,strong)GRNewsModel * newsModel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scollerHeight;



@end

@implementation GRNewsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.wkWebView = [[WKWebView alloc]initWithFrame:CGRectZero configuration: [[WKWebViewConfiguration alloc]init]];
    [self.webView addSubview:self.wkWebView];
    self.wkWebView.navigationDelegate = self;
    self.wkWebView.userInteractionEnabled = NO;

    [self.wkWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.webView);
    }];
    [GRStoreAction getNewsDetail:self.type NewsId:self.newsId ComplectionHander:^(BOOL success, id responseObject, GRError *error) {
        NSLog(@"%@",responseObject);
        if (success) {
            self.newsModel = [GRNewsModel yy_modelWithDictionary:responseObject];
            [self updateUI];
        }
    }];
    
}

-(void)updateUI{
    [self.goodImageView sd_setImageWithURL:[NSURL URLWithString:self.newsModel.mainPhoto] placeholderImage:[UIImage imageNamed:@""]];
    self.titleLabel.text = self.newsModel.title;
    [self.storeAvatar sd_setImageWithURL:[NSURL URLWithString:self.newsModel.avatar]];
    self.nickNameLabel.text = self.newsModel.nickname;
    self.storeNameLabel.text = self.newsModel.nickname;
    self.scoreLabel.text = [NSString stringWithFormat:@"%@|评分:%.1lf",self.newsModel.categoryName,self.newsModel.score];
    self.discountLabel.text = [NSString stringWithFormat:@"享%ld优惠",self.newsModel.pointGiveRate];
    [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:self.newsModel.mainPhoto] placeholderImage:[UIImage imageNamed:@""]];
    [self.collectionBt setImage:[UIImage imageNamed:self.newsModel.isFavorite?@"alreadyCollect_1":@"notCollect_1"] forState:UIControlStateNormal];
    
    [self loadBrand:self.newsModel.content];
}
-(void)loadBrand:(NSString *)htmlStr{
    NSString *html = [NSString stringWithFormat:@"<html> \n"
                      "<head> \n"
                      "<style type=\"text/css\"> \n"
                      "body {font-size:15px;}\n"
                      "</style> \n"
                      "</head> \n"
                      "<body>"
                      "<script type='text/javascript'>"
                      "window.onload = function(){\n"
                      "var $img = document.getElementsByTagName('img');\n"
                      "for(var p in  $img){\n"
                      "$img[p].style.width = '100%%';\n"
                      "$img[p].style.height ='auto'\n"
                      "}\n"
                      "}"
                      "</script>%@"
                      "</body>"
                      "</html>",htmlStr];
    
    [_wkWebView loadHTMLString:[NSString stringWithFormat:@"<meta content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;\" name=\"viewport\" />%@<div id=\"testDiv\"></div>", html] baseURL:nil];
}

-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    WEAKSELF
    [webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id data, NSError * _Nullable error) {
        CGFloat height = [data floatValue];
        weakSelf.scollerHeight.constant = height + 484;
    }];

}

- (IBAction)collectionAction:(UIButton *)button{
    [GRHUD showProgress:@"正在收藏"];
    
    [GRStoreAction addFavorite:self.newsModel.id FavoriteType:self.newsModel.type == 0?@"news":@"newsPlatform" ComplectionHander:^(BOOL success, id responseObject, GRError *error) {
        [GRHUD dismissHUD];
        if (success) {
            if ([responseObject boolValue]) {
                [GRHUD showMessage:@"收藏成功"];
                [button setImage:[UIImage imageNamed:@"alreadyCollect_1"] forState:UIControlStateNormal];
       
            }else{
                [GRHUD showMessage:@"取消收藏"];
                [button setImage:[UIImage imageNamed:@"notCollect_1"] forState:UIControlStateNormal];
            }
        }else{
            [GRHUD showMessage:@"收藏失败"];
        }
    }];
    
}


- (IBAction)shareAction:(id)sender {
          [[GRUMengCommon shareUMeng] sharePlatform:self title:self.newsModel.title content:self.newsModel.content image:self.newsModel.shareImage webURL:nil miniApp:YES option:@{@"webpageUrl":(self.newsModel.shareUrl?self.newsModel.shareUrl:@""),@"userName":(self.newsModel.originalId?self.newsModel.originalId:@""),@"path":(self.newsModel.appGoodsPage?self.newsModel.appGoodsPage:@""),@"poster":self.self.newsModel.shareImage}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
